/// fix bug when shp is bigger than raster. 2020-6-1
#ifndef WSHPPOLYGON_H
#define WSHPPOLYGON_H

#include "shapefil.h"
#include <vector>
#include <algorithm>
#include <string>
#include <time.h>



using std::string;
using std::vector;

struct WHoriLineSeg {
	int x0, y, x1;
};



struct WShpLineSeg {
	inline WShpLineSeg(double tx0,double ty0,double tx1,double ty1):
		x0(tx0),y0(ty0),x1(tx1),y1(ty1){} ;
	inline WShpLineSeg():
		x0(0),y0(0),x1(0),y1(0){} ;
	double x0, y0;
	double x1, y1;
};

struct WShpPolygon {

	///generate lines from shpfile.
	///@param filepath input xxx.shp
	///@param lines return lines
	static bool readLinesFromShapefile(const char* filepath , vector<WShpLineSeg>& lines);


	///@param lines input in proj coordinates
	///@param segs return hori-segments in image coordinates
	///@param topY input top latitude
	///@param topY input bottom latitude
	///@param resoY input y resolution (generally negative value.)
	static void convertLines2Segments(vector<WShpLineSeg>& lines, vector<WHoriLineSeg>& segs,
		const double topY,const int imageHeight,const double resoY,
		const double leftX,const int imageWidth,const double resoX);


	/// this is used for partial anaylyse , so need leftTop offset postiiton in image pixel coordinates.
	///@param dataBuffer input image data
	///@param partOffsetX left offset x
	///@param partOffsetY top offset y
	///@param partWidth width of this part
	///@param partHeight height of this part
	///@param segs horizonal segments
	///@param minvalid min valid value included
	///@param maxvalid max valid value included
	///@param retMin return valid min in image
	///@param retMax return valid max in image
	///@param retSum return valid sum in image
	///@param retCnt return valid count in image
	// template <typename T>
	// static void computeStatistic(T* dataBuffer,
	// 	const int partOffsetX, const int partOffsetY, const int partWidth, const int partHeight,
	// 	vector<WHoriLineSeg>& segs,
	// 	const double minvalid, const double maxvalid,
	// 	double& retMin, double& retMax, double& retSum, double& retCnt);

	///generate timestampid
	static string generateTimestampId(string prefix);

};




#endif

